<?php
/**
 * Description of SeoConfig
 *
 * @author kingop
 */
class DatabaseObject_SeoConfig extends DatabaseObject {
    public function __construct($db) {
        parent::__construct($db, 'seo_config', 'id');
        
        $this->add('config_name');
        $this->add('config_value');
        $this->add('display_config_name');
    }
    
    public static function UpdateConfigValue($db, $name, $value) {
        $tmp = new DatabaseObject_SeoConfig($db);
        if(!$tmp->load($name, 'config_name'))
            return false;
        
        $tmp->config_value = $value;
        $tmp->save();
        
        return true;
    }
    
    public static function GetConfigValue($db, $name) {
        $tmp = new DatabaseObject_SeoConfig($db);
        $tmp->load($name, 'config_name');
        return $tmp->config_value; 
    }
    
    public static function GetConfig($db) {
        $select = self::_GetBaseQuery($db, array());
        $select->from(null, '*');

        $data = $db->fetchAll($select);
        $config = array();
        
        foreach($data as $item) {
            $config[$item['config_name']] = $item['config_value'];
        }        

        return $config;
    }
    
    public static function GetConfigObject($db) {
        $select = self::_GetBaseQuery($db, array());
        $select->from(null, '*');

        $data = $db->fetchAll($select);
        
        $config = self::BuildMultiple($db, __CLASS__, $data);

        return $config;
    }
    
    private static function _GetBaseQuery($db, $options) {
        // инициализация настроек
        $defaults = array();

        foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }

        // создание запроса на получение
        $select = $db->select();
        $select->from(array('p' => 'seo_config'), array());

        return $select;
    }
}

?>
